<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>

<div class="modal fade" id="studyEditorModal" data-backdrop="static">
	<div class="modal-dialog modal-lg">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only"><?=$this->lang->line("CLOSE")?></span></button>
				<h4 class="modal-title">&nbsp;</h4>
			</div>
			<div class="modal-body">
				<div class="callout callout-danger" id="editorError" style="display:none;"></div>
				<form class="" role="form" autocomplete="off"></form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal"><?=$this->lang->line("CLOSE")?></button>
				<button type="button" class="btn btn-success" data-url=""><span class="glyphicon glyphicon-floppy-disk"></span> <?=$this->lang->line("SAVE")?></button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal fade" id="studySecondaryDisplayModal" data-backdrop="static">
	<div class="modal-dialog modal-lg">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only"><?=$this->lang->line("CLOSE")?></span></button>
				<h4 class="modal-title">&nbsp;</h4>
			</div>
			<div class="modal-body"></div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal"><?=$this->lang->line("CLOSE")?></button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal fade" id="studyDisplayModal" data-backdrop="static">
	<div class="modal-dialog modal-lg">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only"><?=$this->lang->line("CLOSE")?></span></button>
				<h4 class="modal-title">&nbsp;</h4>
			</div>
			<div class="modal-body"></div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal"><?=$this->lang->line("CLOSE")?></button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal modal-warning fade" id="studyConfirmModal" data-backdrop="static">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only"><?=$this->lang->line("CLOSE")?></span></button>
				<h4 class="modal-title">&nbsp;</h4>
			</div>
			<div class="modal-body">
				<form class="" role="form" autocomplete="off"></form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-outline pull-left" data-dismiss="modal"><?=$this->lang->line("CANCEL")?></button>
				<button type="button" class="btn btn-outline" data-url=""><?=$this->lang->line("CONFIRM")?> <span class="glyphicon glyphicon-chevron-right"></span></button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal modal-danger fade" id="studyDeleteModal" data-backdrop="static">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only"><?=$this->lang->line("CLOSE")?></span></button>
				<h4 class="modal-title">&nbsp;</h4>
			</div>
			<div class="modal-body">
				<form class="" role="form" autocomplete="off"></form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-outline pull-left" data-dismiss="modal"><?=$this->lang->line("CANCEL")?></button>
				<button type="button" class="btn btn-outline" data-dismiss="modal" data-url="" data-redirect-url=""><span class="glyphicon glyphicon-remove-circle"></span> <?=$this->lang->line("DELETE")?></button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script>
	$(function() {
		$('#studyEditorModal').on('hidden.bs.modal', function (e) {
			if(e.target.id == 'studyEditorModal') {
				$('#studyEditorModal .modal-body form').children().remove();
				$('#editorError').hide();
				$('#editorError div').html('');
			}
		});

		$('#studyDisplayModal').on('hidden.bs.modal', function (e) {
			$('#studyDisplayModal .modal-body form').children().remove();
		});

		$('#studySecondaryDisplayModal').on('hidden.bs.modal', function (e) {
			$('#studySecondaryDisplayModal .modal-body form').children().remove();
		});

		$('#studyDeleteModal').on('hidden.bs.modal', function (e) {
			$('#studyDeleteModal .modal-body form').children().remove();
		});

		$('#studyEditorModal button.btn-success').on('click', function() {
			$this = $(this);
			if($('#studyEditorModal form [type="file"]').length) {
				$('#studyEditorModal form').attr('enctype', 'multipart/form-data');
				$('#studyEditorModal form').attr('method', 'POST');
				$('#studyEditorModal form').attr('action', $('#studyEditorModal button.btn-success').data('url'));
				$('#studyEditorModal form').submit();
			} else {
				//$('#studyEditorModal form :input').removeAttr('disabled');
				$('#studyEditorModal textarea').each(function() {
					if(typeof(CKEDITOR) != 'undefined' && CKEDITOR.instances[$(this).attr('name')]) {
						$(this).val(CKEDITOR.instances[$(this).attr('name')].getData());
					}
				});
				var data = $('#studyEditorModal form').serialize();
				//$('#studyEditorModal form :input[readonly]').attr('disabled', true);
				$.ajax({
					type: "POST",
					cache: false,
					data: data,
					url: $this.data('url'),
					success: function (result) {
						if(result.trim())
							eval(result);
						else
							document.location.reload(true);
					},
					error: function (error) {}
				});
			}
		});

		$('#studyDeleteModal button.btn-danger').on('click', function() {
			$this = $(this);

			if (!$this.data('url') && typeof callbackDeleteModal === "function") {
				callbackDeleteModal($this);
			} else {
				var data = '';
				if($('#studyDeleteModal').find('form :input').length) {
					$('#studyDeleteModal').find('form :input').attr("disabled", false);
					data = $('#studyDeleteModal').find('form').serialize();
					$('#studyDeleteModal').find('form :input').attr("disabled", true);
				}

				$.ajax({
					type: $('#studyDeleteModal').find('form :input').length ? 'POST' : 'GET',
					cache: false,
					data: data,
					url: $this.data('url'),
					success: function (result) {
						if(result.trim())
							eval(result);
						else {
							if($this.data('redirectUrl')) {
								document.location = $this.data('redirectUrl');
							} else {
								document.location.reload(true);
							}
						}
					},
					error: function (error) {}
				});
			}
		});

		$('#studyConfirmModal button.btn-success').on('click', function() {
			$this = $(this);

			if (typeof callbackConfirmModal === "function") {
				callbackConfirmModal();
			} else {
				$.ajax({
					type: 'POST',
					cache: false,
					data: $('#studyConfirmModal .modal-body form').serialize(),
					url: $this.data('url'),
					success: function (result) {
						if(result.trim())
							eval(result);
						else {
							if($this.data('redirectUrl')) {
								document.location = $this.data('redirectUrl');
							} else {
								document.location.reload(true);
							}
						}
					},
					error: function (error) {}
				});
			}

		});

	});
</script>